<?php

namespace App\Models;

use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\ProductCategory
 * 商品分类表
 *
 * @property int $id
 * @property string $name 分类名称
 * @property string $introduction 分类的介绍
 * @property int $parent_id 父类id
 * @property int $level 级别
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory whereIntroduction($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory whereLevel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory whereParentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductCategory[] $next
 * @property-read int|null $next_count
 * @property int $order 排序的顺序
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductCategory[] $children
 * @property-read int|null $children_count
 * @property-read \App\Models\ProductCategory $parent
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Product[] $product
 * @property-read int|null $product_count
 * @property-read \App\Models\ProductCategory $superior
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductCategory whereOrder($value)
 */
class ProductCategory extends Model
{
    use ModelTree, AdminBuilder;

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->setParentColumn('parent_id');
        $this->setOrderColumn('order');
        $this->setTitleColumn('name');
    }

    protected $fillable = ['name', 'introduction', 'parent_id', 'level'];

    /**
     * 下级的分类
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function next()
    {
        return $this->hasMany(ProductCategory::class, 'parent_id');
    }

    /**
     * 上级分类
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function superior()
    {
        return $this->belongsTo(ProductCategory::class,'parent_id');
    }

    /**
     * 商品
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function product()
    {
        return $this->hasMany(Product::class,'category_id');
    }

    public function skus()
    {

    }
    /**
     * 返回所有的分类按着级别
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public  function  getAllCategory() {
        return self::with('next')->where('level',1)->get();
    }


}
